import sys

from PyQt5.QtCore import QDateTime, QTime, QDate
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QMessageBox
import a025.ljy025 as myForm  # 引入Dt Designer设计的界面
'''
1、使用Qtdesigner设计界面，并pyuic转换为py文件
2、载入转换后的界面.py文件
3、掌握控件 timeEdit dateEdit dateTimeEdit canlendaWidget
        cdt = QDateTime.currentDateTime()
        self.timeEdit.setTime(cdt.time())
        self.dateEdit.setDate(cdt.date())
        self.dateTimeEdit.setDateTime(cdt)
        # fromString()
        self.timeEdit.setTime(QTime.fromString('10:18', 'HH:mm'))
        self.dateEdit.setDate(QDate.fromString('20181115', 'yyyyMMdd'))
        self.dateTimeEdit.setDateTime(QDateTime.fromString('2016-11-12 09:11:12', 'yyyy-MM-dd HH:mm:ss'))

    # 日历组件
    self.calendarWidget.selectionChanged.connect(self.get_calendar)
        selectdate = self.calendarWidget.selectedDate()
        # toString()
        self.lineEdit.setText(selectdate.toString("yyyy年MM月dd日"))
        
'''


class MyClass(QWidget, myForm.Ui_Form):  # 增加继承界面类
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setupUi(self)  # 执行界面类函数
        self.setWindowTitle('日期时间界面')
        self.setWindowIcon(QIcon('./img/msn.ico'))
        # self.setGeometry(800, 370, 700, 300)
        self.timeEdit.setTime(QTime.fromString('10:18', 'HH:mm'))
        self.dateEdit.setDate(QDate.fromString('20181115', 'yyyyMMdd'))
        self.dateTimeEdit.setDateTime(QDateTime.fromString('2016-11-12 09:11:12', 'yyyy-MM-dd HH:mm:ss'))

        # 加载按钮点击事件
        self.pushButton.clicked.connect(self.get_datetime)
        self.calendarWidget.selectionChanged.connect(self.get_calendar)

        self.show()

    def get_datetime(self):
        cdt = QDateTime.currentDateTime()
        self.timeEdit.setTime(cdt.time())
        self.dateEdit.setDate(cdt.date())
        self.dateTimeEdit.setDateTime(cdt)

    def get_calendar(self):
        selectdate = self.calendarWidget.selectedDate()
        print(selectdate.toString('yyyyMMdd'))
        self.lineEdit.setText(selectdate.toString("yyyy年MM月dd日"))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myf = MyClass()
    app.exec_()
